package com.hn.shopadmin.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hn.kiss.modules.sys.shopcommon.common.ParmException;
import com.hn.kiss.modules.sys.shopcommon.util.RxUtil;
import com.hn.kiss.modules.sys.shopdb.entity.Jurisdiction;
import com.hn.kiss.modules.sys.shopdb.entity.JurisdictionExample;
import com.hn.kiss.modules.sys.shopdb.mapper.JurisdictionMapper;
import com.hn.shopadmin.service.JurisdictionService;
import net.logstash.logback.encoder.org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class JurisdictionServiceimpl implements JurisdictionService {
    @Autowired
    private JurisdictionMapper jurisdictionMapper;

    @Override
    public int insertParent(String url, String describes) throws ParmException {
        //校验url是否已存在
        boolean b = validatorUrl("0", url);
        if (b){
            throw new ParmException("url已存在");
        }

        Jurisdiction jurisdiction = new Jurisdiction();
        jurisdiction.setId(RxUtil.getUUID());
        jurisdiction.setParentId("0");
        jurisdiction.setUrl(url);
        jurisdiction.setDescribes(describes);
        return jurisdictionMapper.insert(jurisdiction);
    }

    @Override
    public int insertChild(String url, String parentId, String describes) throws ParmException{
        //校验url是否已存在
        boolean b = validatorUrl(parentId, url);
        if (b){
            throw new ParmException("url已存在");
        }

        Jurisdiction jurisdiction = new Jurisdiction();
        jurisdiction.setId(RxUtil.getUUID());
        jurisdiction.setParentId(parentId);
        jurisdiction.setUrl(url);
        jurisdiction.setDescribes(describes);
        return jurisdictionMapper.insert(jurisdiction);
    }

    @Override
    public int update(String id, String url, String describes) throws ParmException{
        Jurisdiction byId = getById(id);

        //url被修改,校验url是否已存在
        if (!byId.getUrl().equals(url)){
            //校验url是否已存在
            boolean b = validatorUrl(byId.getParentId(), url);
            if (b){
                throw new ParmException("url已存在");
            }
        }

        Jurisdiction jurisdiction = new Jurisdiction();
        jurisdiction.setId(id);
        jurisdiction.setUrl(url);
        jurisdiction.setDescribes(describes);

        return jurisdictionMapper.updateByPrimaryKeySelective(jurisdiction);
    }

    @Override
    public int deleteChild(String id) {
        return jurisdictionMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int deletePrent(String id) {
        int i = jurisdictionMapper.deleteByPrimaryKey(id);

        JurisdictionExample jurisdictionExample = new JurisdictionExample();
        jurisdictionExample.createCriteria().andParentIdEqualTo(id);
        int j = jurisdictionMapper.deleteByExample(jurisdictionExample);

        return i+j;
    }

    @Override
    public Jurisdiction getById(String id) {
        return jurisdictionMapper.selectByPrimaryKey(id);
    }

    @Override
    public PageInfo<Jurisdiction> listPage(Integer page, Integer limit, String parentId, String keywork) {
        PageHelper.startPage(page, limit);
        if (keywork == null){
            keywork = "%%";
        }else{
            keywork = "%" + keywork.trim() + "%";
        }


        JurisdictionExample jurisdictionExample = new JurisdictionExample();
        //排序
        jurisdictionExample.setOrderByClause("url");

        //设置parentId条件
        if (StringUtils.isNotEmpty(parentId)){
            //根据keywork模糊检索url,描述
            jurisdictionExample.createCriteria()
                    .andParentIdEqualTo(parentId)
                    .andUrlLike(keywork);
            jurisdictionExample.or()
                    .andParentIdEqualTo(parentId)
                    .andDescribesLike(keywork);
        }else {//如果父id为空,则表示查询所有子菜单,不包含父菜单
            //根据keywork模糊检索url,描述
            jurisdictionExample.createCriteria()
                    .andParentIdNotEqualTo("0")
                    .andUrlLike(keywork);
            jurisdictionExample.or()
                    .andParentIdNotEqualTo("0")
                    .andDescribesLike(keywork);
        }


        List<Jurisdiction> jurisdictions = jurisdictionMapper.selectByExample(jurisdictionExample);
        return new PageInfo<Jurisdiction>(jurisdictions);
    }

    @Override
    public boolean validatorUrl(String parentId, String url) {
        JurisdictionExample jurisdictionExample = new JurisdictionExample();
        jurisdictionExample.createCriteria()
                .andParentIdEqualTo(parentId)
                .andUrlEqualTo(url);

        List<Jurisdiction> jurisdictions = jurisdictionMapper.selectByExample(jurisdictionExample);

        if (jurisdictions != null && jurisdictions.size() > 0){
            return true;
        }
        return false;
    }
}
